/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jxtafileshare;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryService;
import net.jxta.peergroup.PeerGroup;
import net.jxta.platform.NetworkManager;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.PipeAdvertisement;

/**
 *
 * @author Danny
 */
public class ShareFiles extends Thread implements DiscoveryListener {
    private NetworkManager manager;
    private PeerGroup peerGroup;
    private DiscoveryService myDiscovery = null;
    private String fName;

    public ShareFiles(NetworkManager manager, PeerGroup peerGroup) {
        this.manager = manager;
        this.peerGroup = peerGroup;

        if(this.peerGroup != null) {
            myDiscovery = this.peerGroup.getDiscoveryService();
        } else {
            System.out.println("Peer group is null");
            System.exit(-1);
        }
    }

    public void share(String wait, File[] files) {
        String value = System.getProperty("RDVWAIT", "false");
        boolean waitForRendezvous = Boolean.valueOf(value);
        for(int i = 0; i < files.length; i++) {
            ShareClient client = new ShareClient(waitForRendezvous, peerGroup,
                    manager, files[i]);
            client.start();
        }
    }

    public void search() {
        boolean found = false;
        Enumeration fileEnumeration = null;
        System.out.println("Trying to find advertisements");
        while(!found) {
            try {
                fileEnumeration = myDiscovery.getLocalAdvertisements(DiscoveryService.ADV,
                        "Desc", "jpg");
            } catch(IOException e) {
                System.out.println("Can't find local adv");
            }
            if (fileEnumeration != null) {
                while(fileEnumeration.hasMoreElements()) {
             System.out.println("Found local advertisement");
                    PipeAdvertisement pipeAdv = null;
                    pipeAdv = (PipeAdvertisement)
                            fileEnumeration.
                            nextElement();
                    if(pipeAdv.getDescription().equals("jpg")) {
                        fName = pipeAdv.getName() + pipeAdv.getDescription();
                        ShareServer server = new ShareServer(peerGroup, pipeAdv, manager,
                                                        fName);
                        server.start();
                        found = true;
                        break;
                    }
                }
            }
            myDiscovery.getRemoteAdvertisements(null, DiscoveryService.ADV,
                            "Desc", "jpg", 10, null);

            if(fName != null)
                found = true;
        }
    }

    public void discoveryEvent(DiscoveryEvent ev) {
        Enumeration remoteFileEnumeration = null;

        DiscoveryResponseMsg myMessage = ev.getResponse();
        remoteFileEnumeration = myMessage.getResponses();
        if(remoteFileEnumeration != null) {
            System.out.println("Remote file advertisement found!");
            while(remoteFileEnumeration.hasMoreElements()) {
                PipeAdvertisement pipeAdv = null;
                pipeAdv = (PipeAdvertisement)
                        remoteFileEnumeration.nextElement();
                if(pipeAdv.getDescription().equals("jpg")) {
                        fName = pipeAdv.getName() + pipeAdv.getDescription();
                        ShareServer server = new ShareServer(peerGroup, pipeAdv, manager,
                                                        fName);
                        server.start();
                }
            }
        }
    }
}
